package com.jiahao.material.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface RoleMapper extends BaseMapper<Role> {

    @Results(id = "roleMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "code",column = "code"),
            @Result(property = "description",column = "description"),
            @Result(property = "status",column = "status"),
            @Result(property = "createTime",column = "createTime"),
            @Result(property = "updateTime",column = "updateTime"),
            @Result(property = "permissions",column = "id",many = @Many(select = "com.jiahao.material.mapper.PermissionMapper.getPermissionsByRid")),
            @Result(property = "menus",column = "id",many = @Many(select = "com.jiahao.material.mapper.MenuMapper.getMenusByRid"))
    })
    @Select("select * from role where id = #{id}")
    public Role getRoleById(String id);

    @ResultMap("roleMap")
    @Select("select * from role where id in (select rid from user_role where uid = #{uid})")
    public List<Role> getRolesByUid(String uid);

    @ResultMap("roleMap")
    @Select({
            "<script>",
            "select * from role",
            "<where>",
            "<if test='map.name != null and map.name!=\"\"'>and name like concat(concat('%',#{map.name},'%'))</if>",
            "<if test='map.code != null and map.code!=\"\"'>and code like concat(concat('%',#{map.code},'%'))</if>",
            "<if test='map.status != null and map.status!=\"\"'>and status = #{map.status}</if>",
            "</where>",
            "</script>"
    })
    public IPage<Role> getByPage(Page<Role> page, @Param("map")Map<String,Object> map);

}
